/**
 * 用户消费
 */
var queryUserConsumptionSearch = function() {
	var queryUserConsumptionFieldSet = new Ext.form.FieldSet({
		id : 'queryUserConsumptionFieldSet',
		title : '查询条件',
		collapsible : true,// 可折叠
		buttonAlign : 'center',
		keys : [{
					key : 13, // 13代表回车
					fn : queryUserConsumption,
					scope : this
				}],
		items : [{
			layout : 'column',
			border : false,
			items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '用户编号',
									name : "userId"
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							layout : 'column',
							border : false,
							items : [{
										text : '查询',
										xtype : 'button',
										width : 80,
										handler : function() {
											queryUserConsumption();
										},
										style : {
											marginLeft : "100px",
											marginRight : "10px"
										}
									}, {
										text : '重置',
										xtype : 'button',
										width : 80,
										handler : function() {
											Ext
													.getCmp('queryUserConsumptionForm')
													.getForm().reset();
										}
									}]
						}]
					}]
		}]
	});
	fieldSetListeners(['queryUserConsumptionFieldSet',
					'queryUserConsumptionForm', 'userConsumptionManagerGrid'],
			[0.14, 0.86]);

	var queryUserConsumptionForm = new Ext.form.FormPanel({
				id : 'queryUserConsumptionForm',
				anchor : '100% 14%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryUserConsumptionFieldSet]
			});
	return queryUserConsumptionForm;
};

/**
 * grids
 */

var userConsumptionManagerGrid = function() {
	var userConsumptionManagerSm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	// 定义表的每列（对应表头信息）
	var userConsumptionManagerCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				dataIndex : "orderId",
				header : "订单编号",
				hidden : true
			}, {
				dataIndex : "userId",
				header : "用户编号",
				sortable : true,
				width : 150
			}, {
				dataIndex : "userName",
				header : "用户名称",
				sortable : true,
				width : 150
			}, {
				dataIndex : "cousumptionNumber",
				header : "购买次数",
				sortable : true,
				width : 150
			}, {
				dataIndex : "totalCousumtion",
				header : "实际支付总金额",
				sortable : true,
				width : 150,
				renderer : function(value) {
					return PmsUtil.Price(value);
				}
			}, {
				header : "管理",
				width : 200,
				dataIndex : "userId",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var l = g.data.userId;
					var orderId = g.data.orderId;
					var k = "";
					if (isGranted("ucManager_查看详情")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="lookUserConsumptionDetail('
								+ l + ')\" >查看详情</a>';
					}
					return k;
				}
			}]);

	// 定义分析器
	var userConsumptionManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "orderId"
						}, {
							name : "userId"
						}, {
							name : "userName"
						}, {
							name : "cousumptionNumber"
						}, {
							name : "totalCousumtion"
						}]
			});

	// 取数据store
	var userConsumptionViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/statisticManager/selectSatasticUserConsumption"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryUserConsumptionForm')
											.getForm().getValues());
						}
					}
				},
				reader : userConsumptionManagerReader
			});

	// 定义grid的toolbar
	var ucManagerTb = new Ext.Toolbar({
				id : "ucManagerTb",
				height : 25,
				items : []
			});
	ucManagerTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : "ucManager_刷新",
				pressed : true,
				handler : function() {
					queryUserConsumption();
				}
			}));
	var userConsumptionManagerGrid = new Ext.grid.EditorGridPanel({
				id : 'userConsumptionManagerGrid',
				store : userConsumptionViewStore,
				cm : userConsumptionManagerCm,
				sm : userConsumptionManagerSm,
				autoScroll : true,
				clicksToEdit : 1,
				loadMask : true,
				trackMouseOver : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 86%',
				frame : true,
				tbar : ucManagerTb,
				bbar : new PG.PagingBar({
							store : userConsumptionViewStore
						})
			});
	return userConsumptionManagerGrid;
};

// 查询
function queryUserConsumption() {
	var grid = Ext.getCmp("userConsumptionManagerGrid");
	var form = Ext.getCmp("queryUserConsumptionForm");
	if (form.getForm().isValid()) {
		var gridStore = grid.getStore();
		gridStore.baseParams = Ext.apply(gridStore.baseParams, form.getForm()
						.getValues());
		gridStore.load();
	}
}

function lookUserConsumptionDetail(userId) {
	var centerPanel = App.getContentPanel();
	var tab = centerPanel.getItem(PmsUtil.USER_CONSUMPTION_DETAIL);
	var createNewTab = new CreatNewTab(PmsUtil.USER_CONSUMPTION_DETAIL,
			'用户消费详情');
	if (tab == null) {
		createNewTab.createNew(tab);
	} else {
		createNewTab.createOld(tab, ['userConsumptionDetailGrid']);
	}
	Ext.Loader.load([__ctxPath + '/js/statistics/userConsumptionDetail.js'],
			function() {
				Ext.getCmp("centerTabPanel").getActiveTab()
						.add(lookUserConsumptionDetailGrid(userId));
				Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
			});
}

Ext.getCmp("centerTabPanel").getActiveTab().add(queryUserConsumptionSearch(),
		userConsumptionManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
